package com.cdream.basic.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

/**
 * @author cdream
 * @date 2022/10/22
 */
@Slf4j
public class RedisClient {
    public static void main(String[] args) throws InterruptedException {
        final byte[] Line = {'\r','\n'};
        NioEventLoopGroup group = new NioEventLoopGroup();
        Channel channel = new Bootstrap()
                .group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new LoggingHandler());
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                ByteBuf buffer = ctx.alloc().buffer();
                                buffer.writeBytes("*3".getBytes());
                                buffer.writeBytes(Line);
                                buffer.writeBytes("$3".getBytes(StandardCharsets.UTF_8));
                                buffer.writeBytes(Line);
                                buffer.writeBytes("set".getBytes(StandardCharsets.UTF_8));
                                buffer.writeBytes(Line);
                                buffer.writeBytes("$3".getBytes(StandardCharsets.UTF_8));
                                buffer.writeBytes(Line);
                                buffer.writeBytes("dog".getBytes(StandardCharsets.UTF_8));
                                buffer.writeBytes(Line);
                                buffer.writeBytes("$8".getBytes(StandardCharsets.UTF_8));
                                buffer.writeBytes(Line);
                                buffer.writeBytes("yetiandi".getBytes(StandardCharsets.UTF_8));
                                buffer.writeBytes(Line);
                                ctx.writeAndFlush(buffer);
                            }

                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                               log.info(((ByteBuf)msg).toString(StandardCharsets.UTF_8));
                            }
                        });
                    }
                })
                .connect(new InetSocketAddress("localhost", 6379))
                .sync()
                .channel();
        ChannelFuture future = channel.closeFuture();
        future.addListener((f)-> {
            log.info("我优雅的关闭了");
            group.shutdownGracefully();
        });
        // channel.close();

    }
}
